﻿using FileUpload;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CFileUploadCodeplex.Controllers
{
    public class HomeController : Controller
    {
        // HomeController.cs
        public ActionResult Index()
        {
            ViewBag.FileId = Guid.NewGuid(); return View();
        }

        public PartialViewResult UploadFile(Guid id)
        {
            ViewBag.FileId = id; return PartialView();
        }

        [AjaxNoCache]
        public ActionResult Status(Guid id)
        {
            return Json(new { isok = true, message = CFileUpload.CheckProgress(id) }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// Get this in an external file. This is an attribute that will ensure the ajax callresponse is not cached
        /// by the browser
        /// </summary>
        public class AjaxNoCacheAttribute : ActionFilterAttribute
        {
            public override void OnResultExecuted(ResultExecutedContext filterContext)
            {
                if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
                    filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
                    filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    filterContext.HttpContext.Response.Cache.SetNoStore();
                }
            }
        }

        [HttpPost]
        public string SaveFile()
        {
            // you can't useRequest["id"], but you can take the id from the url, and based on theid and the folder you
            // were saving the file, you can copy that file wereveryou want
            return "Uploadcomplete";
        }

    }
}
